﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewstorePlateformeMobileApp.Controllers
{
    public class ReceptionController : Controller
    {
        //
        // GET: /reception/

        public ActionResult Index()
        {
            if (SessionHelper.GetSession(SessionHelper.sessionVariables.iUTILIS_ID) == null)
                Redirect("/Connexion");
            return View();
        }

        public ActionResult Enregistrer()
        {
            if (SessionHelper.GetSession(SessionHelper.sessionVariables.iUTILIS_ID) == null)
                Redirect("/Connexion");
            return View();
        }

        [HttpPost]
        public ActionResult CreerMouvement(string gtin, string emplacement)
        {
            try
            {
                int? utilisId = (int?)SessionHelper.GetSession(SessionHelper.sessionVariables.iUTILIS_ID);

                if (utilisId == null)
                {
                    return Json(new string[2] { "-1", "Utilisateur inconnu !" });
                }

                bool created = Models.Reception.CreerMouvement((int)utilisId, gtin, emplacement);

                if (created)
                {
                    return Json(new string[2] { "1", "Marchandise enregistrée." });
                }
                else
                {
                    return Json(new string[2] { "0", "Marchandise non enregistrée !" });
                }
            }
            catch (Exception ex)
            {
                return Json(new string[2] { "-1", ex.Message });
            }
        }

        [HttpPost]
        public ActionResult LancerTache()
        {
            try
            {
                int? utilisId = (int?)SessionHelper.GetSession(SessionHelper.sessionVariables.iUTILIS_ID);

                if (utilisId == null)
                {
                    return Json(new string[2] { "-1", "Utilisateur inconnu !" });
                }

                bool started = Models.Reception.LancerTache((int)utilisId);

                if (started)
                {
                    return Json(new string[2] { "1", "Tâche démarrée." });
                }
                else
                {
                    return Json(new string[2] { "0", "Tâche non démarrée !" });
                }
            }
            catch (Exception ex)
            {
                return Json(new string[2] { "-1", ex.Message });
            }
        }
    }
}
